package com.ynding.cloud.auth.server.admin.service.api.impl;

import com.ynding.cloud.auth.api.admin.api.IRoleApiService;
import com.ynding.cloud.auth.server.admin.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p> </p>
 *
 * @author ynding
 * @version 2021/9/10
 **/
@DubboService(version = "1.0.0", timeout = 5000)
@Slf4j
@RequiredArgsConstructor
public class RoleApiServiceImpl implements IRoleApiService {

    private final IUserService userService;

    @Override
    public Set<String> findRolesByUsername(String username) {
        //用户被授予的角色资源
        UserDetails user = userService.loadUserByUsername(username);
        Set<String> RoleNameSet = user.getAuthorities().stream().map(m -> m.getAuthority()).collect(Collectors.toSet());
        if (log.isDebugEnabled()) {
            log.debug("用户被授予角色的资源数量是:{}, 资源集合信息为:{}", RoleNameSet.size(), RoleNameSet);
        }
        return RoleNameSet;
    }
}
